package com.utc.cards.table.jade.agent.hostAgent;

import jade.core.Agent;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import android.content.Context;
import android.content.Intent;

import com.utc.cards.Constants;
import com.utc.cards.model.HostModel;
import com.utc.cards.model.game.InfoType;

public class HostAgent extends Agent implements IHostAgent
{

    private static final long serialVersionUID = -8649418473966240098L;
    private static Logger log = LoggerFactory.getLogger(HostAgent.class);

    private HostModel model;
    private Context context;

    @Override
    protected void setup()
    {
        super.setup();
        Object[] args = getArguments();
        if (args != null && args.length > 0)
        {
            // try catch for host test only, we don't have access to Context
            // class in the Cards-host project since it runs in windows
            try
            {
                if (args[0] instanceof Context)
                {
                    context = (Context) args[0];
                }
                else
                {
                    log.error("Missing Context arg during agent setup");
                }
                if (args[1] instanceof HostModel)
                {
                    model = (HostModel) args[1];
                }
                else
                {
                    log.error("Missing HostModel arg during agent setup");
                }
            }
            catch (Exception e)
            {
                // e.printStackTrace();
            }
        }

        // Behaviour d'inscription au jeu
        log.debug("add behaviour SubscriptionBehaviour to HostAgent");
        addBehaviour(new SubscriptionBehaviour(this));
        addBehaviour(new PlayerActivityListenerBehaviour(this));

        // expose l'interface pour la rendre accessible par les activity
        registerO2AInterface(IHostAgent.class, this);
    }

    // envoie le nom du jeu à tous les joueurs
    @Override
    public void sendGameSelected()
    {
        log.debug("add behaviour InfoBehaviour to HostAgent (game selection)");
        addBehaviour(new InfoBehaviour(this, InfoType.GAME_SELECTION, model.getGame().getName(), model.getPlayersMap().values()));
    }

    @Override
    public void createIAPlayer()
    {
        // @See TableLaunchGameActivity.java

    }

    @Override
    public void removeIAPlayer()
    {
        // NOT IMPLEMENTED

    }

    @Override
    public Context getContext()
    {
        return context;
    }

    @Override
    public HostModel getModel()
    {
        return model;
    }

    @Override
    public void onPlayerJoin()
    {
        Intent intent = new Intent(Constants.PLAYER_LIST);
        context.sendBroadcast(intent);
    }

}
